package com.dhwl.hsh.merchant.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dhwl.hsh.lib.utils.StringUtils;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.common.Constant;
import com.dhwl.hsh.merchant.view.PercentAddSubEditView;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 计费方式设置页面
 *
 * @author Derek.Kidd
 * @version V1.0.0
 * @date 2016/5/19 10:09
 */
public class BillSettingActivity extends BaseActivity {


    @Bind(R.id.tv_bill_title)
    TextView tvBillTitle;
    @Bind(R.id.edit_bill_time_price)
    EditText editBillTimePrice;
    @Bind(R.id.tv_shipping_price1)
    TextView tvShippingPrice1;
    @Bind(R.id.layout_bill_time)
    LinearLayout layoutBillTime;
    @Bind(R.id.pase_percentage)
    PercentAddSubEditView pasePercentage;
    @Bind(R.id.edit_truck_price1)
    EditText editTruckPrice1;
    @Bind(R.id.tv_shipping_price2)
    TextView tvShippingPrice2;
    @Bind(R.id.layout_bill_price)
    LinearLayout layoutBillPrice;
    @Bind(R.id.btn_unit_piece)
    Button btnUnitPiece;
    @Bind(R.id.btn_unit_cube)
    Button btnUnitCube;
    @Bind(R.id.edit_unit_price)
    EditText editUnitPrice;
    @Bind(R.id.edit_truck_price2)
    EditText editTruckPrice2;
    @Bind(R.id.tv_shipping_price3)
    TextView tvShippingPrice3;
    @Bind(R.id.layout_bill_unit)
    LinearLayout layoutBillUnit;

    @Bind(R.id.tv_unit)
    TextView tvUnit;
    @Bind(R.id.tv_unit_price)
    TextView tvUnitPrice;


    @Bind(R.id.btn_sure)
    Button btnSure;

    /**
     * 计费方式
     */
    int billType;
    /**
     * 总运费
     */
    String totalPrice;

    /**
     * 百分比
     */
    double percent;
    /**
     * 每车货价格
     */
    double truckPrice;

    /**
     * 每件货物价格
     */
    double unitPrice;

    /**
     * 件数
     */
    int unitNum;

    /**
     * 0 ：按件 1：按立方
     */
    int unitType = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bill_setting;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setTitleContent("计费方式");
        billType = getIntent().getIntExtra(Constant.UserInfo.KEY_BILL_TYPE, Constant.Order.VALUE_BILL_TYPE_2);
        switch (billType) {
            case Constant.Order.VALUE_BILL_TYPE_2://按趟计费   （仅限周期订单）
                tvBillTitle.setText(R.string.bill_type_time);
                layoutBillTime.setVisibility(View.VISIBLE);
                layoutBillPrice.setVisibility(View.GONE);
                layoutBillUnit.setVisibility(View.GONE);
                break;
            case Constant.Order.VALUE_BILL_TYPE_3://按货价计费（仅限周期订单）
                tvBillTitle.setText(R.string.bill_type_price);
                layoutBillTime.setVisibility(View.GONE);
                layoutBillPrice.setVisibility(View.VISIBLE);
                layoutBillUnit.setVisibility(View.GONE);
                break;
            case Constant.Order.VALUE_BILL_TYPE_4://按件数计费（仅限周期订单）
                tvBillTitle.setText(R.string.bill_type_unit);
                layoutBillTime.setVisibility(View.GONE);
                layoutBillPrice.setVisibility(View.GONE);
                layoutBillUnit.setVisibility(View.VISIBLE);

                setUnitChoose();
                break;
        }
        editBillTimePrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                totalPrice = StringUtils.getPrice(editBillTimePrice.getText().toString());
                tvShippingPrice1.setText(totalPrice);
            }
        });

        editTruckPrice1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                percent = pasePercentage.getNum();
                String price1 = editTruckPrice1.getText().toString().trim();
                if (!StringUtils.isEmpty(price1)) {
                    truckPrice = Double.valueOf(price1);
                    double total = truckPrice * percent;
                    totalPrice = StringUtils.getPrice(total);
                } else {
                    totalPrice = StringUtils.getPrice(0);
                }
                tvShippingPrice2.setText(totalPrice);
            }
        });
        editTruckPrice2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                getToalShipPrice();
            }
        });
        editUnitPrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                getToalShipPrice();
            }
        });
    }

    /**
     * 单位计费 总价计算
     */
    private void getToalShipPrice() {
        String price2 = editTruckPrice2.getText().toString().trim();
        String pieceNum = editUnitPrice.getText().toString().trim();

        if (!StringUtils.isEmpty(price2) && !StringUtils.isEmpty(pieceNum)) {
            unitNum = Integer.parseInt(pieceNum);
            unitPrice = Double.valueOf(price2);
            double total = unitPrice * unitNum;
            totalPrice = StringUtils.getPrice(total);
        } else {
            totalPrice = StringUtils.getPrice(0);
        }
        tvShippingPrice3.setText(totalPrice);
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.btn_sure)
    public void onClick() {
        Intent intent = new Intent();
        // 根据计费方式获取计费参数
        switch (billType) {
            case Constant.Order.VALUE_BILL_TYPE_2://按趟计费   （仅限周期订单）
                if (Double.valueOf(totalPrice) > 0) {
                    intent.putExtra("shipping_price", Double.valueOf(totalPrice));//按趟计费: 每趟配送货物运费
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    showToast("请输入金额");
                    return;
                }
                break;
            case Constant.Order.VALUE_BILL_TYPE_3://按货价计费（仅限周期订单）
                if (Double.valueOf(totalPrice) > 0) {
                    intent.putExtra("truck_price", truckPrice);//按货价计费：平均每车货物价格
                    intent.putExtra("truck_percent", percent);//按货价计费：每车货价百分比
                    intent.putExtra("shipping_price", Double.valueOf(totalPrice));//按趟计费: 每趟配送货物运费
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    showToast("请输入金额");
                    return;
                }
                break;
            case Constant.Order.VALUE_BILL_TYPE_4://按件数计费（仅限周期订单）

                if (StringUtils.isEmpty(editTruckPrice2.getText().toString())) {
                    showToast("请输入单位货价");
                    return;
                }
                if (StringUtils.isEmpty(editUnitPrice.getText().toString())) {
                    showToast("请输入单位数量");
                    return;
                }

                if (Double.valueOf(totalPrice) > 0) {
                    intent.putExtra("unit_type", unitType);//按件数计费：0：件 1：立方

                    intent.putExtra("unit_price", unitPrice);//按件数计费：作为运费的价格
                    intent.putExtra("unit_num", unitNum);//按件数计费：总件数
                    intent.putExtra("shipping_price", Double.valueOf(totalPrice));//按趟计费: 每趟配送货物运费
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    showToast("请输入有效金额");
                    return;
                }
                break;
        }
    }

    @OnClick({R.id.btn_unit_piece, R.id.btn_unit_cube})
    void onUnitBtnClick(View view) {
        if (view.getId() == R.id.btn_unit_piece) {
            unitType = 0;
            tvUnit.setText("件");
            tvUnitPrice.setText("按每件货物价格");
        } else {
            unitType = 1;
            tvUnit.setText("立方");
            tvUnitPrice.setText("按每立方货物价格");
        }
        setUnitChoose();
    }

    /**
     * 单位计费 类型选择
     */
    private void setUnitChoose() {
        if (unitType == 0) {
            btnUnitPiece.setTextColor(getResources().getColor(R.color.color_ff9b1a));
            btnUnitPiece.setBackgroundResource(R.drawable.bg_extra_service_checked);
        } else {
            btnUnitCube.setTextColor(getResources().getColor(R.color.color_ff9b1a));
            btnUnitCube.setBackgroundResource(R.drawable.bg_extra_service_checked);
        }
    }
}
